package org.system.core.controller.system;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.system.core.annotation.Log;
import org.system.core.common.PageResult;
import org.system.core.common.Result;
import org.system.core.domain.entity.SysDictData;
import org.system.core.enums.BusinessType;
import org.system.core.service.system.SysDictDataService;
import org.system.core.utils.PageUtils;
import org.system.core.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author baoRoot
 */
@RestController
@Api(tags = {"字典数据模块"})
@RequestMapping("/system/dict/data")
public class SysDictDataController {

    private static final Logger logger = LoggerFactory.getLogger(SysDictDataController.class);

    @Autowired
    private SysDictDataService sysDictDataService;

    @GetMapping("/list")
    @ApiOperation("获取字典数据列表")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    public PageResult<SysDictData> list(SysDictData dictData) {
        logger.info("获取字典数据列表，{}", dictData);
        PageUtils.startPage();
        List<SysDictData> list = sysDictDataService.selectDictDataList(dictData);
        PageInfo<SysDictData> pageInfo = new PageInfo<>(list);
        return PageResult.ok(pageInfo);
    }

    @GetMapping("/{dictCode}")
    @ApiOperation("查询字典数据详细")
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    public Result<SysDictData> getDictInfo(@PathVariable Long dictCode) {
        logger.info("查询字典数据详细: {}", dictCode);
        return Result.ok(sysDictDataService.selectDictDataById(dictCode));
    }

    @GetMapping("/type/{dictType}")
    @ApiOperation("根据字典类型查询字典数据信息")
    public Result<List<SysDictData>> dictTypeList(@PathVariable String dictType) {
        logger.info("根据字典类型查询字典数据信息: {}", dictType);
        List<SysDictData> list = sysDictDataService.selectDictDataByType(dictType);
        if (StringUtil.isNull(list)) {
            list = new ArrayList<>();
        }
        return Result.ok(list);
    }

    @PostMapping
    @ApiOperation("新增字典类型数据")
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    public Result<String> addDictData(@Validated @RequestBody SysDictData dict) {
        logger.info("新增字典类型数据: {}", dict);
        sysDictDataService.insertDictData(dict);
        return Result.ok();
    }

    @PutMapping
    @ApiOperation("更新字典类型数据")
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    public Result<String> editDictData(@Validated @RequestBody SysDictData dict) {
        logger.info("更新字典类型数据： {}", dict);
        sysDictDataService.updateDictData(dict);
        return Result.ok();
    }

    @ApiOperation("删除字典类型数据")
    @DeleteMapping("/{dictCodes}")
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    public Result<String> deleteDictData(@PathVariable Long[] dictCodes) {
        logger.info("删除字典类型数据： {}", dictCodes);
        sysDictDataService.deleteDictDataByIds(dictCodes);
        return Result.ok();
    }

}
